import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:guochaoapp/controller/tabbar_controller.dart';
import 'package:guochaoapp/r.dart';
import 'package:guochaoapp/routes/app_pages.dart';
import 'package:guochaoapp/services/user_service.dart';

class GCTabbarPage extends StatelessWidget {

  final c = Get.put(GCTabbarController());

  @override
  Widget build(BuildContext context) {
    final double _tabbarItemWith = MediaQuery
        .of(context)
        .size
        .width / 4;
    final double _itemHeight = 59;

    _tabbarBox(Widget child) {
      return SizedBox(
        width: _tabbarItemWith,
        height: _itemHeight,
        child: child,
      );
    }

    _navigationBarItem(String imageName, String selImageName, String title,
        int index) {
      return Obx(() => GestureDetector(
        onTap: () {
          if(index == 1 || index == 2 || index == 3) {
            UserService userService = Get.find();
            if(userService.isLogin == false) {
              Get.toNamed(Routes.LOGIN);
              return;
            }
          }
          c.updateIndex(index);
        },
        child: Container(
          color: Colors.transparent,
          child: Column(
            children: <Widget>[
              SizedBox(height: 8),
              Image.asset(
                c.currentIndex.value == index ? selImageName : imageName,
                width: 25,
                height: 25,
              ),
              SizedBox(height: 2),
              Text(
                  title,
                  style: c.currentIndex.value == index
                      ? Theme
                      .of(context)
                      .tabBarTheme
                      .labelStyle
                      : Theme
                      .of(context)
                      .tabBarTheme
                      .unselectedLabelStyle),
            ],
          ),
        ),
      ));
    }

    return RepaintBoundary(
      child: Scaffold(
          bottomNavigationBar: BottomAppBar(
            color: Colors.white,
            shape: CircularNotchedRectangle(),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                _tabbarBox(_navigationBarItem(
                    R.assetsIconTabHome, R.assetsIconTabHomeSel, '首页', 0)),
                _tabbarBox(_navigationBarItem(
                    R.assetsIconTabMall, R.assetsIconTabMallSel, '商城', 1)),
                _tabbarBox(_navigationBarItem(
                    R.assetsIconTabCart, R.assetsIconTabCartSel, '购物车', 2)),
                _tabbarBox(_navigationBarItem(
                    R.assetsIconTabMine, R.assetsIconTabMineSel, '我的', 3))
              ],
            ),
          ),
          body: PageView(
            children: c.pages,
            controller: c.controller,
            onPageChanged: (index) {
              c.currentIndex.value = c.controller.page.round();
            },
            physics: NeverScrollableScrollPhysics(),
          )),
    );
  }
}

